package com.lu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lu.dao.BlogCommentMapper;
import com.lu.dao.BlogTagRelationMapper;
import com.lu.pojo.BlogComment;
import com.lu.pojo.BlogInfo;
import com.lu.dao.BlogInfoMapper;
import com.lu.pojo.BlogTagRelation;
import com.lu.service.BlogInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 博客信息表 服务实现类
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-31
 */
@Service
public class BlogInfoServiceImpl extends ServiceImpl<BlogInfoMapper, BlogInfo> implements BlogInfoService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Autowired
    private BlogTagRelationMapper blogTagRelationMapper;

    @Autowired
    private BlogCommentMapper blogCommentMapper;

    // 这个类里面的方法抛出异常，就会回滚，数据库里面的数据也会回滚。
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean clearBlogInfo(Long blogId) {

        // 判断数据库操作是否成功
        if (SqlHelper.retBool(blogInfoMapper.deleteById(blogId))) {
            // 文章的tag标签删除
            QueryWrapper<BlogTagRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(BlogTagRelation::getBlogId, blogId);
            blogTagRelationMapper.delete(queryWrapper);
            // 文章的评论删除
            QueryWrapper<BlogComment> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(BlogComment::getBlogId, blogId);
            blogCommentMapper.delete(wrapper);
            return true;
        }
        return false;
    }

}
